﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.DHQG
{
    public class MocKiemSoatDAO : SqlDAO
    {
        public MocKiemSoatDAO(string connectionString, System.Type type = null) :
            base(connectionString, type) { ;}

        //public const string PROCEDURE_GET_LIST_PAGING = "spChiTieu_GetListPaging";
        public const string PROCEDURE_INSERT_UPDATE = "spMocKiemSoat_InsertUpdate";
        public const string PROCEDURE_DELETE = "spMocKiemSoat_DELETE";
        public const string PROCEDURE_GET_BY_ID = "spMocKiemSoat_GetByID";
        public const string PROCEDURE_GET_BY_ID_NEW = "spMocKiemSoat_GetByID_NEW";
        public const string PROCEDURE_CHECK_CHOT = "spMocKiemSoat_CheckThucHien";
        public const string PROCEDURE_CHECK_CHOT_BY_BAN = "spMocKiemSoat_CheckThucHien_BY_BAN";
        public const string PROCEDURE_GET_before_NKH = "spNamkehoch_Getbefore";
        public const string PROCEDURE_GET_IS_ACTIVE = "spMocKiemSoat_IS_ACTIVE";
        public const string PROCEDURE_CHECK_BAN_CHOT = "spMocKiemSoat_CheckBanChot";
        public const string PROCEDURE_BEFO = "spMocKiemSoat_GetBefo";

        public const string PARAM_MOC_KIEM_SOAT_ID = "@MOC_KIEM_SOAT_ID";
        public const string PARAM_MOC_KIEM_SOAT = "@MOC_KIEM_SOAT";
        public const string PARAM_THOI_HAN = "@THOI_HAN";
        public const string PARAM_NAM_KE_HOACH_ID = "@NAM_KE_HOACH_ID";
        public const string PARAM_IS_ACTIVE = "@IS_ACTIVE";
        public const string PARAM_DON_VI_ID = "@DON_VI_ID";


        public int Save(MocKiemSoat model)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT_UPDATE);
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT_ID, model.MOC_KIEM_SOAT_ID));
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT, model.MOC_KIEM_SOAT));
                cmd.Parameters.Add(CreateParameter(PARAM_THOI_HAN, model.THOI_HAN));
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, model.NAM_KE_HOACH_ID));
                cmd.Parameters.Add(CreateParameter(PARAM_IS_ACTIVE, model.IS_ACTIVE));
                return Convert.ToInt32(ExecuteScalar(cmd));
            }
            catch
            {
                throw;
            }
        }
        public List<NAMDV> CheckChotByMocKS(int namkh, int mocks)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_CHECK_CHOT);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkh));
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT_ID, mocks));
                return GetList<NAMDV>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public List<NAMDV> CheckChotByMocKSByBan(int namkh, int mocks, int donviid)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_CHECK_CHOT_BY_BAN);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkh));
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT_ID, mocks));
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, donviid));
                return GetList<NAMDV>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public List<NAMDV> CheckBanChotByMocKS(int namkh, int mocks)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_CHECK_BAN_CHOT);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkh));
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT_ID, mocks));
                return GetList<NAMDV>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public MocKiemSoat GetMocKSIsActive(int namkh)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_IS_ACTIVE);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkh));

                return GetSingle<MocKiemSoat>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public MocKiemSoat Getmksbefo(int namkh, int mocks)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_BEFO);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkh));
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT_ID, mocks));
                return GetSingle<MocKiemSoat>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public bool Delete(int id)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE);
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT_ID, id));
                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public int GetNamkehoch_Getbefore(int namkehoachid)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_before_NKH);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkehoachid));
                return Convert.ToInt32(ExecuteScalar(cmd));
            }
            catch
            {
                throw;
            }
        }
        public List<MocKiemSoat> GetbyNamkehoach(int namkehoachid)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_ID);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkehoachid));

                return GetList<MocKiemSoat>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public MocKiemSoat GetbyID(int id)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_ID_NEW);
                cmd.Parameters.Add(CreateParameter(PARAM_MOC_KIEM_SOAT_ID, id));

                return GetSingle<MocKiemSoat>(cmd);
            }
            catch
            {
                throw;
            }
        }
    }
}
